﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Xml;
using System.Xml.Linq;
using System.Data;

namespace RESTful.DAO
{
    public class FeedPersonal_DAO : DataProvider
    {
        public FeedPersonal_DAO()
        {
            Connect();
        }

        public object ConvertDataTableToArray(DataTable dt, int i)
        {
            DTO.FeedPersonal_DTO data = new DTO.FeedPersonal_DTO();
            data.MaFeedPersonal = Convert.ToInt32(dt.Rows[i]["MaFeedPersonal"].ToString());
            data.Link = dt.Rows[i]["Link"].ToString();
            return (object)data;
        }

        public DTO.FeedPersonal_DTO[] GetLink(String ID)
        {
            try
            {
                String sql = "Select * From FeedPersonal Where TinhTrang = 1 and IDAccess = '" + ID + "'";
                DataTable dt = ExecuteQuery(sql);
                DTO.FeedPersonal_DTO[] ds = new DTO.FeedPersonal_DTO[dt.Rows.Count];
                for (int i = 0; i < dt.Rows.Count; i++)
                    ds[i] = (DTO.FeedPersonal_DTO)ConvertDataTableToArray(dt, i);
                return ds;
            }
            catch
            {
                DisConnect();
                return null;
            }
        }

        public void Insert(Stream s)
        {
            try
            {
                using (XmlReader r = XmlReader.Create(s))
                {
                    XElement d = XElement.Load(r);
                    String sql = "Insert into FeedPersonal(IDAccess, Link, Date, TinhTrang)";
                    sql += " values('" + d.Element("IDAccess").Value + "','" + d.Element("Link").Value + "','" + DateTime.Now + "', 1)";
                    ExecuteNonQuery(sql);
                }
            }
            catch
            {
                DisConnect();
                return;
            }
        }

        public void Delete(String MaFeed)
        {
            try
            {                
                String sql = "Update FeedPersonal Set TinhTrang = 0 Where MaFeedPersonal = " + MaFeed;
                ExecuteNonQuery(sql);                                    
            }
            catch
            {
                DisConnect();
                return;
            }
        }

        public void Update(Stream s)
        {
            try
            {
                using (XmlReader r = XmlReader.Create(s))
                {
                    XElement d = XElement.Load(r);
                    String sql = "Update FeedPersonal Set Date = '" + DateTime.Now + "', ";
                    sql += " Link = '" + d.Element("Link") + "'";
                    sql += " Where MaFeedPersonal = '" + d.Element("MaFeedPersonal").Value + "'";
                    ExecuteNonQuery(sql);
                }
            }
            catch
            {
                DisConnect();
                return;
            }
        }

        public List<DTO.DataFeed_DTO> list = new List<DTO.DataFeed_DTO>();

        public object ConvertFeed(DataTable dt, int i)
        {
            DTO.DataFeed_DTO data = new DTO.DataFeed_DTO();
            data.Title = dt.Rows[i]["Title"].ToString();
            data.Link = dt.Rows[i]["Link"].ToString();
            data.Description = dt.Rows[i]["Description"].ToString();
            data.PubDate = dt.Rows[i]["PubDate"].ToString();
            return (object)data;
        }

        public void HandleFeed(String url)
        {
            XmlTextReader reader = null;
            try
            {
                reader = new XmlTextReader(url);
                DataSet ds = new DataSet();
                ds.ReadXml(reader);
                DataTable dt = ds.Tables["item"];
                DTO.DataFeed_DTO[] data = new DTO.DataFeed_DTO[dt.Rows.Count];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    data[i] = (DTO.DataFeed_DTO)ConvertFeed(dt, i);
                    list.Add(data[i]);
                }
                reader.Close();
            }
            catch (Exception ex)
            {
                ex.Message.ToString();
            }
        }

        public DTO.DataFeed_DTO[] GetFeed(String ID)
        {
            try
            {
                String sql = "Select * From FeedPersonal";
                sql += " Where TinhTrang = 1 and IDAccess = '" + ID + "'";
                List<DTO.FeedPersonal_DTO> listDM = new List<DTO.FeedPersonal_DTO>();
                DataTable dt = ExecuteQuery(sql);
                for (int i = 0; i < dt.Rows.Count; i++)
                    listDM.Add((DTO.FeedPersonal_DTO)ConvertDataTableToArray(dt, i));
                for (int i = 0; i < listDM.Count; i++)
                    HandleFeed(listDM[i].Link.ToString());

                DTO.DataFeed_DTO[] data = new DTO.DataFeed_DTO[list.Count];
                for (int i = 0; i < list.Count; i++)
                    data[i] = (DTO.DataFeed_DTO)ConvertListToArray(i);
                return data;
            }
            catch
            {
                DisConnect();
                return null;
            }
        }

        public object ConvertListToArray(int i)
        {
            DTO.DataFeed_DTO data = new DTO.DataFeed_DTO();
            data.Link = list[i].Link.ToString();
            data.PubDate = list[i].PubDate.ToString();
            data.Title = list[i].Title.ToString();
            data.Description = list[i].Description.ToString();
            return (object)data;
        }
    }
}